<template>
  <div>
    <div>
      <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
        <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-house" style="font-weight: bold">&nbsp;&nbsp;后台管理</i>
        </el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-s-marketing">&nbsp;&nbsp;销售管理</i></el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-s-marketing">&nbsp;&nbsp;订单发票</i></el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="data-canRead" style="width: 100%;background-color: white;height: auto">
      <el-divider content-position="left">信息查询</el-divider>
<!--      <el-form ref="form" :model="oderInvoiceForm">-->
<!--        <el-form-item class="oderInvoice_formSubmit_search">-->
<!--          <el-select v-model="oderInvoiceForm.commodityName" placeholder="请选择资产名称"-->
<!--                     @change="loadoderInvoiceCommodityNameNList()">-->
<!--            <el-option-->
<!--                v-for="item in oderInvoiceOptions"-->
<!--                :key="item.id"-->
<!--                :label="item.commodityName"-->
<!--                :value="item.id">-->
<!--            </el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
<!--      </el-form>-->
      <el-table :data="oderInvoice.slice((currentPage-1)*pageSize,currentPage*pageSize)" border style="width: 100%">
        <el-table-column prop="commodityCode" label="商品编码" width="200px" align="center"></el-table-column>
        <el-table-column prop="commodityName" label="商品名称" width="200px" align="center"></el-table-column>
        <el-table-column prop="amount" label="订单金额" width="200px" align="center"></el-table-column>
        <el-table-column prop="specs" label="规格" width="200px" align="center"></el-table-column>
        <el-table-column prop="companyName" label="供应商名称" width="200px"  align="center"></el-table-column>
        <el-table-column prop="count" label="数量" width="200px" align="center"></el-table-column>
<!--        <el-table-column prop="invoice" label="发票" width="200px" align="center">-->
<!--          <template slot-scope="scope">-->
<!--            <img :src="scope.row.invoice" width="40" height="40">-->
<!--          </template>-->
<!--        </el-table-column>-->

        <el-table-column label="操作" width="200px" fixed="right">
          <template slot-scope="scope">
            <el-button
                type="primary"
                circle size="mini"
                @click="openEditDialog(scope.row)"><i class="el-icon-edit" style="color: white"></i></el-button>
            <el-button
                type="danger"
                circle size="mini"
                @click="openDeleteConfirm(scope.row)"><i class="el-icon-delete" style="color: white"></i></el-button>
          </template>
        </el-table-column>

      </el-table>

      <!-- 以下是弹出的修改申请的对话框 -->
      <el-dialog title="编辑申请" :visible.sync="dialogFormVisible" :modal-append-to-body="false">
        <el-form :model="oderInvoiceForm">
          <el-form-item label="商品编码" :label-width="formLabelWidth">
            <el-input v-model="oderInvoiceForm.commodityCode" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="商品名称" :label-width="formLabelWidth">
            <el-input v-model="oderInvoiceForm.commodityName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="订单金额" :label-width="formLabelWidth">
            <el-input v-model="oderInvoiceForm.amount" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="规格" :label-width="formLabelWidth">
            <el-input v-model="oderInvoiceForm.specs" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="供应商名称" :label-width="formLabelWidth">
            <el-input v-model="oderInvoiceForm.companyName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="数量" :label-width="formLabelWidth">
            <el-input v-model="oderInvoiceForm.count" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="发票" :label-width="formLabelWidth">
            <el-input v-model="oderInvoiceForm.invoice" autocomplete="off"></el-input>
          </el-form-item>



        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitEditForm()">确 定</el-button>
        </div>
      </el-dialog>

<!--没事-->
      <div style="margin-top:15px;">
        <el-pagination align='center' @size-change="handleSizeChange"
                       @current-change="handleCurrentChange"
                       :current-page="currentPage"
                       :page-sizes="[1,5,10,20]"
                       :page-size="pageSize"
                       layout="total, sizes, prev, pager, next, jumper"
                       :total="oderInvoice.length">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dialogFormVisible: false,
      formLabelWidth: '120px',

      oderInvoice: [],
      oderInvoiceForm: {
        commodityCode: '',
        commodityName: '',
        amount: '',
        specs: '',
        companyName: '',
        count: '',
        // invoice: '',
      },
      currentPage: 1,
      pageSize: 10,
    }
  },
  methods: {
    /*加载表格*/
    loadoderInvoiceForm() {
      let url = 'http://localhost:9080/purchaseOderandinvoices/list';
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          let companyList = responseBody.data;
          this.oderInvoice = companyList;
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },
    // /*加载搜索固定资产名*/
    // loadoderInvoiceCommodityNameNList() {
    //   let url = 'http://localhost:9080/'
    //       + this.oderInvoiceOptions.id;
    //   console.log('url = ' + url);
    //   this.axios
    //       .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
    //       .get(url).then((response) => {
    //     let responseBody = response.data;
    //     if (responseBody.state == 20000) {
    //       this.oderInvoiceForm = responseBody.data;
    //     } else {
    //       this.$message.error(responseBody.message);
    //     }
    //   });
    // },
    /*表单编辑*/
    openEditDialog(oderInvoice) {
      let url = 'http://localhost:9080/purchaseOderandinvoices/' + oderInvoice.id;
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.dialogFormVisible = true;
          this.oderInvoiceForm = responseBody.data;
          console.log(this.oderInvoice)
        } else {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadoderInvoiceForm();
            }
          });
        }
      });
    },

    submitEditForm() {
      let url = 'http://localhost:9080/purchaseOderandinvoices/' + this.oderInvoiceForm.id + '/update';
      console.log('url = ' + url);
      let formData = this.qs.stringify(this.oderInvoiceForm);
      console.log('formData = ' + formData);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url, formData).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: '修改发票成功！',
            type: 'success'
          });
          this.loadoderInvoiceForm();
          this.dialogFormVisible = false;
        } else if (responseBody.state == 40400) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
              this.loadoderInvoiceForm();
            }
          });
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },
    /*表单删除*/
    openDeleteConfirm(oderInvoice) {
      let title = '提示';
      let message = '此操作将永久删除【' + oderInvoice.companyName + '】属性, 是否继续?';
      this.$confirm(message, title, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(oderInvoice);
      }).catch(() => {
      });
    },
    handleDelete(oderInvoice) {
      let url = 'http://localhost:9080/purchaseOderandinvoices/' + oderInvoice.id + '/delete';
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: '删除属性【' + oderInvoice.companyName + '】成功！',
            type: 'success'
          });
          this.loadoderInvoiceForm();
        } else {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadoderInvoiceForm();
            }
          });
        }
      });
    },
    /*表格*/
    //每页条数改变时触发 选择一页显示多少行
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.currentPage = 1;
      this.pageSize = val;
    },
    //当前页改变时触发 跳转其他页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
    },
  },
  mounted() {
    this.loadoderInvoiceForm();
  }
}
</script>

<style scoped>
* {
  color: black;
}

.data-canRead {
  width: 380px;
  height: 380px;
  margin-top: 15px;
  box-shadow: 0px 5px 5px #8d858a;
  float: left;
  margin-right: 16px;
}

.oderInvoice_formSubmit_search {
  width: 200px;
  float: left;
  right: 10px;
  background-color: white;
}

</style>